package com.qiyuan.web.controller.essay;

import java.util.List;
import java.util.Arrays;

import com.qiyuan.common.core.page.TableDataInfo;
import com.qiyuan.essay.domain.bo.BlogTypeBo;
import com.qiyuan.essay.domain.vo.BlogTypeVo;
import com.qiyuan.essay.service.IBlogTypeService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.qiyuan.common.annotation.RepeatSubmit;
import com.qiyuan.common.annotation.Log;
import com.qiyuan.common.core.controller.BaseController;
import com.qiyuan.common.core.domain.PageQuery;
import com.qiyuan.common.core.domain.AjaxResult;
import com.qiyuan.common.core.validate.AddGroup;
import com.qiyuan.common.core.validate.EditGroup;
import com.qiyuan.common.core.validate.QueryGroup;
import com.qiyuan.common.enums.BusinessType;
import com.qiyuan.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;

/**
 * 文章类型Controller
 *
 * @author ruoyi
 * @date 2022-04-16
 */
@Validated
@Api(value = "文章类型控制器", tags = {"文章类型管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/essay/type")
public class BlogTypeController extends BaseController {

    private final IBlogTypeService iBlogTypeService;

    /**
     * 查询文章类型列表
     */
    @ApiOperation("查询文章类型列表")
    @SaCheckPermission("essay:type:list")
    @GetMapping("/list")
    public TableDataInfo<BlogTypeVo> list(@Validated(QueryGroup.class) BlogTypeBo bo, PageQuery pageQuery) {
        return iBlogTypeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出文章类型列表
     */
    @ApiOperation("导出文章类型列表")
    @SaCheckPermission("essay:type:export")
    @Log(title = "文章类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated BlogTypeBo bo, HttpServletResponse response) {
        List<BlogTypeVo> list = iBlogTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "文章类型", BlogTypeVo.class, response);
    }

    /**
     * 获取文章类型详细信息
     */
    @ApiOperation("获取文章类型详细信息")
    @SaCheckPermission("essay:type:query")
    @GetMapping("/{id}")
    public AjaxResult<BlogTypeVo> getInfo(@ApiParam("主键")
                                                  @NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Integer id) {
        return AjaxResult.success(iBlogTypeService.queryById(id));
    }

    /**
     * 新增文章类型
     */
    @ApiOperation("新增文章类型")
    @SaCheckPermission("essay:type:add")
    @Log(title = "文章类型", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody BlogTypeBo bo) {
        return toAjax(iBlogTypeService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改文章类型
     */
    @ApiOperation("修改文章类型")
    @SaCheckPermission("essay:type:edit")
    @Log(title = "文章类型", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody BlogTypeBo bo) {
        return toAjax(iBlogTypeService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除文章类型
     */
    @ApiOperation("删除文章类型")
    @SaCheckPermission("essay:type:remove")
    @Log(title = "文章类型" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Integer[] ids) {
        return toAjax(iBlogTypeService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
